import { getToken } from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"
  
// 页面白名单
const whiteList = [
  '/pages/login', '/pages/register', '/pages/common/webview/index','/pages/index', '/pages/mine/index','/pages/shop/index','/subPackage_1/pages/goods/good-detail',
  '/subPackage_1/pages/expertScience/expert-detail','/subPackage_1/pages/expertScience/science-detail','/pages/shop/search','/pages/shop/pointsIndex'
  // '/pages/identityVerify', '/pages/verify/smartDevicesVerify','/pages/verify/medicalVerify','/pages/verify/familyVerify',
  
  // '/subPackage_1/pages/mine/userInfo', '/subPackage_1/pages/index/health-manager', '/subPackage_1/pages/index/add-family',
  // '/subPackage_1/pages/index/healthData/blood-sugar','/subPackage_1/pages/index/healthData/add-blood-sugar',
  // '/pages/expertScience/index','/subPackage_1/pages/expertScience/expert-detail','/subPackage_1/pages/expertScience/science-detail',
 
  // '/subPackage_1/pages/mine/points/my-points','/subPackage_1/pages/mine/points/points-detail','/subPackage_1/pages/mine/points/points-rule',
  
  // ,'/subPackage_1/pages/goods/good-share',
  // '/subPackage_1/pages/mine/address/address-list',
  // '/subPackage_1/pages/order/order-list','/subPackage_1/pages/order/order-detail'
]

// 检查地址白名单
function checkWhite(url) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
  uni.addInterceptor(item, {
    invoke(to) {
      if (getToken()) {
        if (to.url === loginPage) {
          uni.reLaunch({ url: "/" })
        }
        return true
      } else {
        if (checkWhite(to.url)) {
          return true
        }
        uni.reLaunch({ url: loginPage })
        return false
      }
    },
    fail(err) {
      console.log(err)
    }
  })
})
